# -*- coding: utf-8 -*-
import json
from openerp import models, fields, api

class Mytodo(models.Model):
    _name = 'nmbw800.mymodel'#不能用大写
    #描述
    name=fields.Char(compute='_compute_name')
    #数组字符串
    TotalTable=fields.Char('总量曲线',required=True)#总量曲线 单位10克 1234 表示 12.34kg  20个数据
    TimeTStart=fields.Char('饲喂时间开始',required=True)#饲喂时间开始 10个数据
    TimeTEnd=fields.Char('饲喂时间结束',required=True)#饲喂时间结束 10个数据
    TimeTAmount=fields.Char('饲喂总量',required=True)#饲喂总量 10个数据
    Addr=fields.Char('控制器地址',required=True)#控制器地址
    CalTable=fields.Char('校正表',required=True)#校正表 -- g 10个数据
    CanOver=fields.Integer(string='允许超出量',required=True)#允许超出量-- 10%
    XlPer=fields.Integer(string='每次下料多少克',required=True)#每次下料多少克 -- 100g
    WaterAuto=fields.Integer(string='是否自动下水',required=True)#是否自动下水
    WaterTime=fields.Integer(string='下水时间s',required=True)#下水时间s
    WaterSpace=fields.Integer(string='下水间隔s',required=True)#下水间隔s
    UsePass=fields.Integer(string='是否使用密码',required=True,help='0-不使用,1-使用')#是否使用密码 0 - 不使用 1 - 使用

    RouteName=fields.Integer(string='路由器名称',required=True)#路由器名称
    RoutePass=fields.Integer(string='路由器密码',required=True)#路由器密码
    # #数组字符串
    ServerIP=fields.Char('服务器IP地址',required=True)#服务器IP地址 4个
    ServerPort=fields.Integer(string='服务器端口',required=True)#服务器端口
    Stage=fields.Char(string='母猪状态',required=True)#0 - 断奶 1 - 产前 2 - 哺乳
    Day=fields.Integer(string='第几日',required=True)#第几日
    HasEat=fields.Integer(string='今天已吃g',required=True)#今天已吃
    EatDelay=fields.Integer(string='要求喂食延迟',required=True)#要求喂食延迟
    Sons=fields.Integer(string='仔猪头数',required=True)#仔猪头数
    Mday=fields.Integer(string='保存日',required=True)#保存日
    Rev=fields.Integer(string='保留',required=True)#保留
    Password=fields.Integer(string='用户密码',required=True)#用户密码
    Sum=fields.Integer(string='校验和',required=True)#校验和

    farm=fields.Many2one('nmbw800.farmmodel','对应猪场')#对应的猪场
    # wendu=fields.Integer(string='温度',required=True)

    #计算描述
    @api.multi
    def _compute_name(self):
        for record in self:
            record.name = record.Addr
    # @api.model
    # def addItem(self,name):
    #     print "ffffffffffffffffffffffffffffffffff"
    #     result=self.env['nmbw800.mymodel'].create({'TotalTable':name,
    #                                                   'wendu':'2222'
    #                                                 })
    #     #下面那个也是可以的
    #     return result.id

    # @api.model
    # def changeItem(self,id,name):
    #     self.browse([id]).write({'name':name})


    # @api.model
    # def deleteItem(self,id):
    #     print "1111111111111"
    #     self.browse([id]).unlink();
    #     print "22222222222"

    @api.model
    def addIntance(self,structJson):
        #将golang传过来的结构体json，解析
        j=json.loads(structJson)
        #数组转字符串
        str_TotalTable = ",".join(str(v) for v in j['TotalTable'])
        str_TimeTStart = ",".join(str(v) for v in j['TimeTStart'])
        str_TimeTEnd = ",".join(str(v) for v in j['TimeTEnd'])
        str_TimeTAmount = ",".join(str(v) for v in j['TimeTAmount'])
        str_CalTable = ",".join(str(v) for v in j['CalTable'])
        str_ServerIP = ",".join(str(v) for v in j['ServerIP'])
        #以地址作为唯一搜索是否已经存在这个实例了，如果存在就更新，如果不存在就创建
        j['Addr']="机器号:"+str(j['Addr'])#和日记录详细下料记录的做法统一
        thelen=len(self.search( [('Addr','=',j['Addr'])] ))
        if thelen==0:
            result=self.env['nmbw800.mymodel'].create({'TotalTable':str_TotalTable,
                                                         'TimeTStart':str_TimeTStart,
                                                         'TimeTEnd':str_TimeTEnd,
                                                         'TimeTAmount':str_TimeTAmount,
                                                         'CalTable':str_CalTable,
                                                         'CanOver':j['CanOver'],
                                                         'XlPer':j['XlPer'],
                                                         'WaterAuto':j['WaterAuto'],
                                                         'WaterTime':j['WaterTime'],
                                                         'WaterSpace':j['WaterSpace'],
                                                         'UsePass':j['UsePass'],
                                                         'Addr':j['Addr'],
                                                         'RouteName':j['RouteName'],
                                                         'RoutePass':j['RoutePass'],
                                                         'ServerIP':str_ServerIP,
                                                         'ServerPort':j['ServerPort'],
                                                         'Stage':j['Stage'],
                                                         'Day':j['Day'],
                                                         'HasEat':j['HasEat'],
                                                         'EatDelay':j['EatDelay'],
                                                         'Sons':j['Sons'],
                                                         'Mday':j['Mday'],
                                                         'Rev':j['Rev'],
                                                         'Password':j['Password'],
                                                         'Sum':j['Sum']
                                                        })
            return "新建实例成功Id："+str(result.id)
        if thelen > 0:
            self.search( [('Addr','=',j['Addr'])] ).write({'TotalTable':str_TotalTable,
                                                         'TimeTStart':str_TimeTStart,
                                                         'TimeTEnd':str_TimeTEnd,
                                                         'TimeTAmount':str_TimeTAmount,
                                                         'CalTable':str_CalTable,
                                                         'CanOver':j['CanOver'],
                                                         'XlPer':j['XlPer'],
                                                         'WaterAuto':j['WaterAuto'],
                                                         'WaterTime':j['WaterTime'],
                                                         'WaterSpace':j['WaterSpace'],
                                                         'UsePass':j['UsePass'],
                                                         'Addr':j['Addr'],
                                                         'RouteName':j['RouteName'],
                                                         'RoutePass':j['RoutePass'],
                                                         'ServerIP':str_ServerIP,
                                                         'ServerPort':j['ServerPort'],
                                                         'Stage':j['Stage'],
                                                         'Day':j['Day'],
                                                         'HasEat':j['HasEat'],
                                                         'EatDelay':j['EatDelay'],
                                                         'Sons':j['Sons'],
                                                         'Mday':j['Mday'],
                                                         'Rev':j['Rev'],
                                                         'Password':j['Password'],
                                                         'Sum':j['Sum']
                                                        })
            return "更新实例成功"

        # def test(self,structJson):
        # #json解析
        # j=json.loads(structJson)
        # print j['WaterAuto']
        # #数组转字符串
        # str1 = ",".join(str(v) for v in j['TotalTable'])
        # str2 = ",".join(str(v) for v in j['TimeTStart'])
        # # print str1
        # result=self.env['nmbw800.mymodel'].create({'totalTable':'222222','timeTStart':'222222'})#下面那个也是可以的
        # return result.id


